package cn.iocoder.yudao.module.dlbz.service.suppliermoney;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.dlbz.controller.admin.suppliermoney.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.suppliermoney.SupplierMoneyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dlbz.dal.mysql.suppliermoney.SupplierMoneyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dlbz.enums.ErrorCodeConstants.*;

/**
 * 供应商支付记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SupplierMoneyServiceImpl implements SupplierMoneyService {

    @Resource
    private SupplierMoneyMapper supplierMoneyMapper;

    @Override
    public Long createSupplierMoney(SupplierMoneySaveReqVO createReqVO) {
        // 插入
        SupplierMoneyDO supplierMoney = BeanUtils.toBean(createReqVO, SupplierMoneyDO.class);
        supplierMoneyMapper.insert(supplierMoney);
        // 返回
        return supplierMoney.getId();
    }

    @Override
    public void updateSupplierMoney(SupplierMoneySaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierMoneyExists(updateReqVO.getId());
        // 更新
        SupplierMoneyDO updateObj = BeanUtils.toBean(updateReqVO, SupplierMoneyDO.class);
        supplierMoneyMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplierMoney(Long id) {
        // 校验存在
        validateSupplierMoneyExists(id);
        // 删除
        supplierMoneyMapper.deleteById(id);
    }

    private void validateSupplierMoneyExists(Long id) {
        if (supplierMoneyMapper.selectById(id) == null) {
            throw exception(SUPPLIER_MONEY_NOT_EXISTS);
        }
    }

    @Override
    public SupplierMoneyDO getSupplierMoney(Long id) {
        return supplierMoneyMapper.selectById(id);
    }

    @Override
    public PageResult<SupplierMoneyDO> getSupplierMoneyPage(SupplierMoneyPageReqVO pageReqVO) {
        return supplierMoneyMapper.selectPage(pageReqVO);
    }

}